///<reference path="../typings/index.d.ts"/>
import request = require("request");
var exec = require('child_process').exec;
export class CheckService{
    private serviceUrl:string = "http://127.0.0.1:9938";
    requestStatus():void{
       request(this.serviceUrl,(err,body,content)=>{
           if(err != undefined){
               console.log("reqeust service error" +err);
               this.sendDingding("reqeust serivce error");
               this.CMDAction();
           }else{
               console.log("service runing:"+content);
           }
       });
    }
    start():void{
        setInterval(()=>{
          this.requestStatus();
        },5000);
    }
    sendDingding(str:string):void{
      const url = "https://oapi.dingtalk.com/robot/send?access_token=da9652bda66acb3f84756378671bb9530fba0d92f5ae369355d78903f18abed3";
      const body = {
        msgtype :"text",
        text:{
        content :str}
      };

      request.post(url,{headers : {'Content-Type':'application/json'},body : JSON.stringify(body)},(err,response,body)=>{
          if(err){
              console.log("send dingding faile" + err);
              return;
          }
          console.log(body);

      });
    }
    CMDAction():void{
        exec("forever restartall",(err,stdout,stderr)=>{
            if(err){
                console.log("exec cmd error" + err);
                return;
            }
            console.log("result:"+stdout);
        });
    }
}

var service = new CheckService();
service.start();